import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-08-26
 * Time: 20:00
 */
class MyThreadPool{
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    public MyThreadPool(int n){
        for(int i = 0; i < n; i++){
            Thread t = new Thread(()->{
                try{
                    while(true){
                        Runnable task = queue.take();
                        task.run();
                    }
                }catch (InterruptedException e){
                    throw new RuntimeException(e);
                }

            });
            t.setDaemon(true); //前台线程结束, 后台线程也结束, 整个进程就结束了
            t.start();
        }
    }
    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }
}
public class demo3 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool = new MyThreadPool(10);
        for(int i = 0; i < 100; i++){
            int id = i;
            myThreadPool.submit(()->{
                System.out.println(Thread.currentThread().getName() + id + "线程执行");
            });
        }
        Thread.sleep(1000);
    }
}
